<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="title" label="标题" required>
				<uni-easyinput placeholder="标题" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>
			
			<uni-forms-item name="image" label="封面图">
				<image :src="image" style="width: 600px;height: 400px;" mode="aspectFill"></image>
				<button type="primary" @click="$uploadImg(setFormImg)">上传</button>
				<button type="warn" @click="delimg">删除</button>
				
			</uni-forms-item>
			
			
			<uni-forms-item name="content" label="文章内容" required>

				<editorArticle ref="editor_ref"></editorArticle>

			</uni-forms-item>
			<uni-forms-item name="article_status" label="文章状态">
				<uni-data-checkbox v-model="formData.article_status" :localdata="formOptions.article_status_localdata">
				</uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="sort" label="排序">
				<uni-easyinput type="number" v-model="formData.sort"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="category_id" label="分类">
				<uni-data-checkbox v-model="formData.category_id" collection="news-categories"
					field="name as text, _id as value"></uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/news-articles.js';
	import editorArticle from '@/components/editor-article/editor-article';
	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'news-articles';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		components: {
			editorArticle
		},
		data() {
			let formData = {
				"title": "",
				"content": "",
				"article_status": 0,
				"sort": 0,
				"category_id": "",
				"image":""
			}
			return {
				image:'',
				formData,
				formOptions: {
					"article_status_localdata": [{
							"text": "草稿箱",
							"value": 0
						},
						{
							"text": "已发布",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		watch: {

		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			delimg(){
				this.image = ''
				this.$forceUpdate()
			},
			setFormImg(url) {
				console.log(url)
				
				this.$nextTick(()=>{
					this.image = url 
				})
			},
			/**
			 * 验证表单并提交
			 */
			submit() {


				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				value.content = this.$refs.editor_ref.getEditor().getHtml()
				value.sort = parseInt(value.sort)
				console.log(value)
				value.image = this.image
				// return 

				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>
